# models.py
from odoo import models, fields, api


class WizardBatchCategory(models.TransientModel):
    _name = 'wizard.batch.category'
    _description = 'Wizard Batch Category'

    public_categ_ids = fields.Many2many('product.public.category', 'public_category_batch_category', string='电商类别')
    options = fields.Selection([('add', '添加'), ('unlink', '移除'), ('clean', '清空')], default='add')
    products_ids = fields.Char('产品列表')
    categ_id = fields.Many2one('product.category',string='产品类别')

    def write(self, vals):
        options = vals.get('options') or self.options
        public_categ_ids = vals.get('public_categ_ids')
        if options == 'add':
            if self.products_ids and public_categ_ids:
                prodcuts = self.env['product.product'].browse([int(i) for i in self.products_ids.split(',')])
                prodcuts.write(
                    {'public_categ_ids': [(4, category_id[1]) for category_id in public_categ_ids]}
                )
        elif options == 'unlink':
            if self.products_ids and public_categ_ids:
                prodcuts = self.env['product.product'].browse([int(i) for i in self.products_ids.split(',')])
                prodcuts.write(
                    {'public_categ_ids': [(3, category_id[1]) for category_id in public_categ_ids]}
                )
        elif options == 'clean':
            prodcuts = self.env['product.product'].browse([int(i) for i in self.products_ids.split(',')])
            prodcuts.public_categ_ids = None

        else:
            # Finish the wizard and do something with the data
            pass
        if 'categ_id' in vals:
            self.env['product.product'].browse([int(i) for i in self.products_ids.split(',')]).write(
                {'categ_id': vals['categ_id']}
            )
            # ... more actions ...
        return {
            'type': 'ir.actions.act_window_close',
        }
